package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comments;
import cn.wolfcode.trip.base.mapper.CommentsMapper;
import cn.wolfcode.trip.base.query.CommentQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentsMapper commentsMapper;

    @Override
    public PageInfo query(CommentQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<Comments> list = commentsMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Comments comment) {
        if(comment.getId() != null){
            commentsMapper.updateByPrimaryKey(comment);
        }else {

            comment.setCreate_time(new Date());

            commentsMapper.insert(comment);
        }
    }

    @Override
    public List<Comments> getCommentsByStrategyId(Long strategyId) {
        return commentsMapper.selectCommentsByStrategyId(strategyId);
    }

    @Override
    public List<Comments> getUserByComment(Long id) {
        return commentsMapper.selectUserByComment(id);
    }

    // 其实这里查询所有未读消息个数就行了
    @Override
    public List<Comments> getMessagesByType(Long userId) {
        return commentsMapper.getMessagesByType(userId);
    }

    // 将未读评论设置为已读
    @Override
    public void setMessageState(Integer state) {
        commentsMapper.updateMessageState(state);
    }

    @Override
    public List<Comments> getMessages(Long userId) {
        return commentsMapper.selectMessages(userId);
    }

    /**
     * 查询自己的所有评论
     * @param userId
     * @return
     */
    @Override
    public List<Comments> getMycomments(Long userId) {
        return commentsMapper.selectMycomments(userId);
    }


}
